const app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
var request = require('../../utils/https.js');
var uri = 'index/deal_info.html';
var tocar ='cart/add_cart';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      'http://boss.findtaste.cn/ceshi/public/static/images/g1.png',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg',
       'http://boss.findtaste.cn/ceshi/public/static/images/g1.png'
    ],
    indicatorDots: false,
    autoplay: true,
    circular: false,
    interval: 5000,
    duration: 1000,
    swiperCurrent:0,
    goodsNum:1,
    showModalStatus:false,
    minusStatus: 'disabled',
    totalNum:0,
    goodsId:'',
    goodsInfo:[],
    options:1
  },
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  imageLoad: function (e) {
    var res = wx.getSystemInfoSync();
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      ratio = imgwidth / imgheight;
      this.setData({
        swiperHeight: res.windowWidth / ratio
      })
   },
   /* 下一步 */
  toShopcar:function(e){
    var currentStatu = e.currentTarget.dataset.statu;
    var totalNum = this.data.totalNum;
    var goodsNum = this.data.goodsNum;
    totalNum = parseInt(totalNum)+parseInt(goodsNum);
    this.util(currentStatu);
    this.toCar(totalNum);
  },
  toCart:function(){
    wx.navigateTo({
      url: '../cart/cart',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
   /* 关闭弹窗 */
  closeBox:function(e){
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu);
  },
  /* 立即购物 */
  buyNow:function(e){
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu);
    this.setData({
      options:1
    })
  },
  /* 加入购物车 */
  buyCar: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu);
    this.setData({
      options: 2
    })
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200,  //动画时长  
      timingFunction: "linear", //线性  
      delay: 0  //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.translateY(220).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.translateY(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })

      //关闭  
      if (currentStatu == "close") {
        this.setData(
          {
            showModalStatus: false
          }
        );
      }
    }.bind(this), 200)
    if (currentStatu == "open") {
      this.setData(
        {
          showModalStatus: true
        }
      );
    }
  },
  /* 数量家加减 */
  minNum:function(e){
      var num=this.data.goodsNum;
      if(num >1){
         num --;
      }
      var minusStatus = num <= 1 ? 'disabled' : 'normal';
      // 将数值与状态写回
      this.setData({
        goodsNum: num,
        minusStatus: minusStatus
      });
    },
    maxNum:function(e){
      var num = this.data.goodsNum;
      // 不作过多考虑自增1
      num++;
      // 只有大于一件的时候，才能normal状态，否则disable状态
      var minusStatus = num < 1 ? 'disabled' : 'normal';
      // 将数值与状态写回
      this.setData({
        goodsNum: num,
        minusStatus: minusStatus
      });
    },
    goodsChange: function (e) {
      var num = e.detail.value;
      // 将数值与状态写回
      this.setData({
        goodsNum: num
      });
    },
    getData:function(){
      var that=this;

      request.req(uri, { id: that.data.goodsId, 
        user_id: wx.getStorageSync('userId')},(err,res)=>{
          WxParse.wxParse('detail', 'html', res.data.list.describe, that, 5);
          console.log(res.data.cart_count);
            that.setData({
              totalNum: res.data.cart_count,
              imgUrls:res.data.img,
              goodsInfo:res.data.list
            })
      })
    },
    toCar:function(totalNum){
      var that=this;
      request.req(tocar, {
        deal_id: that.data.goodsId,
        user_id: wx.getStorageSync('userId'),
        number: totalNum},(err,res)=>{
            if(res.data){
              wx.showToast({
                title: '加入成功',
                icon: 'success',
                duration: 1000
              })
              that.setData({
                totalNum: totalNum
              })
              var op = that.data.options;
              if (op==1) {
                console.log(op);
                wx.navigateTo({
                  url: '../cart/cart',
                })
              }
            }else{
              wx.showToast({
                title: '加入失败',
                icon: 'loading',
                duration: 1000
              })
            }
        })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodsId:options.id
    })
    wx.showNavigationBarLoading();
    setTimeout(function(){
      wx.hideNavigationBarLoading();
    },2000)
    this.getData();
    console.log(this.data.totalNum)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})